package com.accp.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.accp.common.DataResult;
import com.accp.entity.Student;
import com.accp.service.StudentService;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;



@RestController
@RequestMapping("/stu")
@Api(tags = "学生接口")
@Slf4j
public class StudentController {

	@Autowired
	StudentService studentService;

	@ApiOperation("查询单个学生")
	@GetMapping("/fetch")
	public DataResult fetchStudent(int id){
		return DataResult.getSuccessResult(this.studentService.fetchStudent(id));
	}

	@ApiOperation("查询全部学生")
	@GetMapping("/all")
	public DataResult getAllStudent(){
		return DataResult.getSuccessResult(this.studentService.getAllStudent());
	}
	
	@ApiOperation("分页查询学生")
	@GetMapping("/page")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageIndex",defaultValue="1",value = "第几页",required=false),
        @ApiImplicitParam(name = "pageRows",defaultValue="3",value = "每页行数",required=false),
        @ApiImplicitParam(name = "gid",defaultValue="0",value = "年级编号",required=false)
	})
	public DataResult getStudentPage(@RequestParam(value = "pageIndex", required = false,defaultValue="1") Integer pageIndex,
			@RequestParam(value = "pageRows", required = false,defaultValue="3") Integer pageRows,
			@RequestParam(value = "gid", required = false,defaultValue="0") Integer gid){
		IPage<Student> ipage = this.studentService.getStudentPage(pageIndex, pageRows, gid);
		log.debug("页数:"+ipage.getPages()+"每页行数:"+ipage.getSize()+"年级编号:"+gid);
		return DataResult.getSuccessResult(ipage);
	}

	@ApiOperation("添加学生")
	@PostMapping("/add")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "学生编号",required=false),
        @ApiImplicitParam(name = "name",value = "姓名",required=true),
        @ApiImplicitParam(name = "grade.gradename",value = "年级名称",required=false),
        @ApiImplicitParam(name = "age",value = "年龄",required=true),
        @ApiImplicitParam(name = "gid",value = "年级id",required=true)
	})
	public DataResult addStudent(Student stu){
		return DataResult.getSuccessResult(this.studentService.addStudent(stu));
		
	}

	@ApiOperation("更新学生")
	@PostMapping("/update")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "学生编号",required=true),
        @ApiImplicitParam(name = "name",value = "姓名",required=true),
        @ApiImplicitParam(name = "grade.gradename",value = "年级名称",required=false),
        @ApiImplicitParam(name = "age",value = "年龄",required=true),
        @ApiImplicitParam(name = "gid",value = "年级id",required=true)
	})
	public DataResult updateStudent(Student stu){
		return DataResult.getSuccessResult(this.studentService.updateStudent(stu));
	}
	
	@ApiOperation("删除学生")
	@GetMapping("/delete")
	public DataResult deleteStudent(int id){
		log.warn("删除学生:"+id);
		return DataResult.getSuccessResult(this.studentService.deleteStudent(id));
	}
}
